package dev.ece.suit.ui;

import java.io.FileNotFoundException;
import java.io.IOException;

import dev.ece.core.shader.DefaultTextureShader;
import dev.ece.core.texture.Texture;
import dev.ece.suit.ui.shader.VariableWidthTextureShader;
import dev.ece.util.lib.IntOffset;
import dev.ece.util.lib.IntRectangle;
import dev.ece.util.obj.ObjectCount;

public class VariableWidthComponent extends Component {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6460600587559330521L;

	private int max;
	
	private int value;
	
	private float offset;
	
	public VariableWidthComponent(String background, float width, float height, IntRectangle centerClipping)
			throws IOException {
		this(background, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(String background, float width, float height, IntOffset centerClipping)
			throws IOException {
		this(background, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(String background, float width, float height, IntRectangle centerClipping, int value, int max)
			throws IOException {
		super(background, width, height, centerClipping);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(String background, float width, float height, IntOffset centerClipping, int value, int max)
			throws IOException {
		super(background, width, height, centerClipping);
		if(centerClipping.isVertical()) {
			throw new IllegalArgumentException("只支持水平宽度变长");
		}
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}

	public VariableWidthComponent(String background, float width, float height) throws IOException {
		this(background, width, height, 100, 100);
	}
	
	public VariableWidthComponent(String background, float width, float height, int value, int max) throws IOException {
		super(background, width, height);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping) throws IOException {
		this(background, textureClipping, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping) throws IOException {
		this(background, textureClipping, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, int value, int max) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}

	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, int value, int max) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		if(centerClipping.isVertical()) {
			throw new IllegalArgumentException("只支持水平宽度变长");
		}
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height)
			throws IOException {
		this(background, textureClipping, width, height, 100, 100);
	}
	
	public VariableWidthComponent(String background, IntRectangle textureClipping, float width, float height, int value, int max)
			throws IOException {
		super(background, textureClipping, width, height);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height, IntRectangle centerClipping)
			throws IOException {
		this(background, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height, IntOffset centerClipping)
			throws IOException {
		this(background, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height, IntRectangle centerClipping, int value, int max)
			throws IOException {
		super(background, width, height, centerClipping);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height, IntOffset centerClipping, int value, int max)
			throws IOException {
		super(background, width, height, centerClipping);
		if(centerClipping.isVertical()) {
			throw new IllegalArgumentException("只支持水平宽度变长");
		}
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}

	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height) throws IOException {
		this(background, width, height, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, float width, float height, int value, int max) throws IOException {
		super(background, width, height);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping) throws IOException {
		this(background, textureClipping, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping) throws IOException {
		this(background, textureClipping, width, height, centerClipping, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntRectangle centerClipping, int value, int max) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}

	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height,
			IntOffset centerClipping, int value, int max) throws IOException {
		super(background, textureClipping, width, height, centerClipping);
		if(centerClipping.isVertical()) {
			throw new IllegalArgumentException("只支持水平宽度变长");
		}
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height)
			throws IOException {
		this(background, textureClipping, width, height, 100, 100);
	}
	
	public VariableWidthComponent(ObjectCount<Texture> background, IntRectangle textureClipping, float width, float height, int value, int max)
			throws IOException {
		super(background, textureClipping, width, height);
		this.value = value;
		this.max = max;
		this.setupVariableWidthComponent();
	}
	
	protected void setupVariableWidthComponent() {
		float length = getWidth() - getValue() * getWidth() / getMax();
		setOffset(length / 2);
	}

	@Override
	protected DefaultTextureShader createDefaultTextureShader() {
		try {
			return VariableWidthTextureShader.getDefaultVariableWidthTextureShader();
		} catch (FileNotFoundException e) {
			LOG.error("加载shader异常", e);
		}
		return null;
	}

	@Override
	protected void glUseShader() {
		super.glUseShader();
		((VariableWidthTextureShader) getDefaultTextureShader()).glOffset(offset);
	}
	
	public float getOffset() {
		return offset;
	}

	protected void setOffset(float offset) {
		this.offset = offset;
	}

	public int getMax() {
		return max;
	}

	protected void setMax(int max) {
		this.max = max;
	}

	public int getValue() {
		return value;
	}

	protected void setValueValue(int value) {
		this.value = value > getMax() ? getMax() : (value < 0 ? 0 : value);
	}
	
	public void setValue(int value) {
		this.setValueValue(value);
		this.setupVariableWidthComponent();
	}

}
